package org.spider;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil {
	public static void saveResponseToFile(String fileName, String sUrl,
			Map<String, String> headers) throws MalformedURLException {
		int bytesum = 0;
		int byteread = 0;

		URL url = new URL(sUrl);

		try {
			URLConnection conn = url.openConnection();

			for (Entry<String, String> entry : headers.entrySet()) {
				// String key = entry.getKey().toString();
				// String value = entry.getValue().toString();
				// System.out.println("key=" + key + " value=" + value);
				conn.addRequestProperty(entry.getKey(), entry.getValue());
			}

			InputStream inStream = conn.getInputStream();
			FileOutputStream fs = new FileOutputStream(fileName);

			byte[] buffer = new byte[4096];
			while ((byteread = inStream.read(buffer)) != -1) {
				bytesum += byteread;
				fs.write(buffer, 0, byteread);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
